/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef GSCONNECTION_H
#define GSCONNECTION_H

#include <gsystems/gsCore.h>
#include <gsystems/gsSocket.h>
#include <gsystems/gsAddress.h>

class gsConnection
{
public:
	/**
	 * 	Default constructor
	 */
	gsConnection();

	/**
	 * 	Constructor which sets this connection's end-point to \a address
	 * @param address The address which will act as an end-point for this connection
	 */
	gsConnection( const gsAddress address );
	
	/**
	 * 	Sets the end-point for this connection to the address specified by \a address
	 * @param address The remote host that this object should connect to
	 */
	void setAddress( const gsAddress address );
	
	/**
	 * 	Causes the gsConnection object to attempt to establish a connection to its specified end-point
	 * @return 	- True if the connection was successfully established
	 *		- False otherwise
	 */
	gsBool establishConnection();
	
	/**
	 * 	Attempts to send data to the remote host at the end-point of this connection
	 * @param dataBuffer The buffer containing the data to be sent
	 * @param bufferSize The size of the data buffer
	 * @return The number of bytes sent to the end-point
	 */
	const gsInt sendData( const void* dataBuffer, const gsUInt bufferSize );
	
	
	/**
	 * 	Attempts to receive data from the remote host at the end-point of this connection
	 * @param dataBuffer The buffer into which received data should be stored
	 * @param bufferSize The size of the data buffer
	 * @return The number of bytes received from the end-point
	 */
	const gsInt receiveData( void* dataBuffer, const gsUInt bufferSize );
	
	/**
	 *	Attempts to break the connection between us and the end-point
	 */
	void breakConnection();
	
    	/**
    	 * 	Default destructor
    	 */
    	~gsConnection();
	
protected:
	
	friend class gsConnectionManager;
	
	/**
	 *	Retrieves the socket handle held by the gsSocket object associated with this connection
	 * @return This connection's socket handle or GS_NULL if the socket has not been initialized
	 */
	gsInt getHandle();
	
	enum gsConnectionStates
	{
		GS_CONNECTION_UNINITIALIZED,
  		GS_CONNECTION_ENDPOINT_SET,
    		GS_CONNECTION_DISCONNECTED,	
    		GS_CONNECTION_CONNECTED,
	};
	
	gsUInt m_state;
	
	gsAddress m_endPoint;
	
	gsSocket *m_socket;

};

#endif
